package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
   private StudentDao studentDao= new StudentDao();
    /**(做逻辑判断的)
     * 添加学生
     * @return
     * 创建studentDao(库管)
     * 将学生对象传递给StudentDao哭观众的addStudent方法
     * 将返回的boolean类型的结果返还给StudentController
     */
    public  boolean addStudent(Student stu) {
       //StudentDao studentDao= new StudentDao();
        return studentDao.addStudent(stu);
            }

            public boolean isExists(String id) {

                Student[]stus=studentDao.findAllStudent();
                //假设在数组中不没有该id
                boolean exists=false;
                for (int i = 0; i < stus.length; i++) {
                    Student student = stus[i];
                    if (student!=null&&student.getId().equals(id)){
                        exists=true;
                        break;
            }
        }
        return exists;
    }

    /**
     * 业务员的查询方法
     * @return
     * 调用库管对象的findAllStudent获取学生对相爱那个数组
     * 判断数组中是否有学生信息(有:返回地址,没有返回null)
     */
    public Student[] findAllStudent() {
        StudentDao studentDao= new StudentDao();
        Student[] allStudent = studentDao.findAllStudent();
        boolean flag=false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu = allStudent[i];
            if (stu!=null){
                flag=true;
                break;
            }
        }
        if(flag){
            return allStudent;
        }else {
            return null;
        }
    }

    /**
     * 删除学生(业务层)
     * @param delId
     */
    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    /**
     * 修改学生信息
     * @param updateId
     * @param newStu
     */
    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);
    }
}
